package com.hundsun.fcp.webplugin.widget.webview;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.webkit.ConsoleMessage;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.EditText;

import com.hundsun.fcp.common.logger.AndroidLogger;
import com.hundsun.fcp.common.logger.Logger;

public class FcpWebChromeClient extends WebChromeClient {
	private static final Logger LOGGER = AndroidLogger.getLogger();
	
	@Override
	public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
		String message = consoleMessage.message();
        int lineNumber = consoleMessage.lineNumber();
        String sourceID = consoleMessage.sourceId();
        String messageLevel = consoleMessage.message();
        LOGGER.debug(String.format("[%s] sourceID: %s lineNumber: %n message: %s",
                messageLevel, sourceID, lineNumber, message)) ;
		return super.onConsoleMessage(consoleMessage);
	}

	@Override
	public boolean onJsPrompt(WebView view, String url, String message,
			String defaultValue, final JsPromptResult result) {
		try {
			AlertDialog.Builder _Builder = new AlertDialog.Builder(view.getContext());
			_Builder.setTitle("提示信息").setMessage(message);
			final EditText localEditText = new EditText(view.getContext());
			localEditText.setSingleLine();
			localEditText.setText(defaultValue);
			_Builder.setView(localEditText);
			_Builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface pInterface,int pAnonymousInt) {
							result.confirm(localEditText.getText().toString());
						}
					}).setNeutralButton("取消",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface pInterface,int pAnonymousInt) {
							result.cancel();
						}
					});
			_Builder.setCancelable(false);
			_Builder.create().show();
			return true;
		} catch (Exception e) {
			LOGGER.error("onJsAlert", e);
		}
		return super.onJsPrompt(view, url, message, defaultValue, result);
	}

	@Override
	public boolean onJsConfirm(WebView view, String url, String message,
			final JsResult result) {
		try {
			AlertDialog.Builder _Builder = new AlertDialog.Builder(
					view.getContext());
			_Builder.setTitle("提示信息")
					.setMessage(message)
					.setPositiveButton("确定",new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface pInterface,int pAnonymousInt) {
									result.confirm();
								}
							})
					.setNeutralButton("取消",new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface pInterface,int pAnonymousInt) {
									result.cancel();
								}
							});
			_Builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
				public void onCancel(DialogInterface pInterface) {
					result.cancel();
				}
			});
			_Builder.setCancelable(false);
			_Builder.create().show();
			return true;
		} catch (Exception e) {
			LOGGER.error("onJsAlert", e);
		}
		return super.onJsConfirm(view, url, message, result);
	}

	@Override
	public boolean onJsAlert(WebView view, String url, String message,
			JsResult result) {
		try {
			AlertDialog.Builder _Builder = new AlertDialog.Builder(view.getContext());
			_Builder.setTitle("提示信息").setMessage(message).setPositiveButton("确定", null);
			_Builder.setCancelable(false);
			_Builder.create().show();
			result.confirm();
			return true;
		} catch (Exception e) {
			LOGGER.error("onJsAlert", e);
		}
		return super.onJsAlert(view, url, message, result);
	}
}
